package cn.wynu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class BookAspect {
    // 切点,可复用
    @Pointcut(value = "execution(* cn.wynu.service.BookService.*(..))")
    public void databaseOperation() { }

    //@Before 在目标方法执行之前执行
    @Before("databaseOperation()")
    public void validUser(JoinPoint joinPoint) {
        String stars = "*".repeat(30);
        System.out.println(stars);

        // 获取方法签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取方法名
        String methodName = signature.getName();
        // 获取返回类型
        Class<?> returnType = signature.getReturnType();
        // 获取参数类型
        Class<?>[] parameterTypes = signature.getParameterTypes();

        System.out.println("方法名: " + methodName);
        System.out.println("返回类型: " + returnType);
        System.out.println("参数类型: " + Arrays.toString(parameterTypes));

        // 获取参数值
        Object[] args = joinPoint.getArgs();
        System.out.println("参数值: " + Arrays.toString(args));

        // 获取目标对象
        Object target = joinPoint.getTarget();
        System.out.println("目标对象: " + target.getClass().getName());

        // 获取代理对象
        Object proxy = joinPoint.getThis();
        System.out.println("代理对象: " + proxy.getClass().getName());

        System.out.println("系统检测权限");
    }

    @After("databaseOperation()")
    public void recordLog() {
        System.out.println("记录日志");
        String stars = "*".repeat(30);
        System.out.println(stars);
    }
}
